package com.appkubes.mall.restful.application.user.account.history.service.impl;

import com.appkubes.base.core.entity.vo.UserKeyVO;
import com.appkubes.mall.core.common.constants.MallUserAccountHistoryConstants;
import com.appkubes.mall.core.entity.MallUserAccountHistory;
import com.appkubes.mall.core.service.impl.BaseMallUserAccountHistoryServiceImpl;
import com.appkubes.mall.restful.application.user.account.account.entity.dto.AccountHistoryDTO;
import com.appkubes.mall.restful.application.user.account.account.entity.dto.AccountStatisDTO;
import com.appkubes.mall.restful.application.user.account.account.entity.dto.OrderPayUserAccountDTO;
import com.appkubes.mall.restful.application.user.account.account.entity.dto.StatisBasic;
import com.appkubes.mall.restful.application.user.account.account.service.UserAccountService;
import com.appkubes.mall.restful.application.user.account.history.dao.UserAccountHistoryDao;
import com.appkubes.mall.restful.application.user.account.history.service.UserAccountHistoryService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: Rongjie
 * @Date: 2020/1/13 21:57
 * @Description:
 */
@Service
@Data
@Slf4j
public class UserAccountHistoryServiceImpl extends BaseMallUserAccountHistoryServiceImpl implements UserAccountHistoryService {

    @Resource
    UserAccountHistoryDao accountHistoryDao;

    @Resource
    UserAccountService accountService;

    @Override
    public AccountStatisDTO getTodayStatis(UserKeyVO userKeyVO) {
        List<StatisBasic> statisBasicList = accountHistoryDao.accountStatis(userKeyVO.getUserKey());

        AccountStatisDTO accountStatisDTO = new AccountStatisDTO();
        if (statisBasicList.size() != 0) {
            StatisBasic statisBasic = statisBasicList.get(0);
            accountStatisDTO.setThisMonthOrderCount(statisBasic.getOrderCount());
            accountStatisDTO.setThisMonthBrokerage(statisBasic.getTotalBrokerage());
        }
        if (statisBasicList.size() == 2) {
            StatisBasic statisBasic1 = statisBasicList.get(1);
            if (statisBasic1 != null) {
                accountStatisDTO.setTodayOrderCount(statisBasic1.getOrderCount());
                accountStatisDTO.setTodayBrokerage(statisBasic1.getTotalBrokerage());
            }
        }
        //如果未来这里有性能问题，请使用CQRS原则，调整会非实时计算，存储统计结果到account表中
        return accountStatisDTO;
    }

    @Override
    public List<AccountHistoryDTO> geHistory(String userKey, MallUserAccountHistoryConstants.State state) {
        return accountHistoryDao.geHistory(userKey, state.name());
    }

    @Override
    public Boolean onOrderPay(OrderPayUserAccountDTO orderPayUserAccountDTO) {
        MallUserAccountHistory mallUserAccountHistory = new MallUserAccountHistory();
        BeanUtils.copyProperties(orderPayUserAccountDTO, mallUserAccountHistory);

        mallUserAccountHistory.setState(MallUserAccountHistoryConstants.State.CONFIRM.name());//未确认发放
        mallUserAccountHistory.setCreateTime(new Date());
        mallUserAccountHistory.setSourceTime(new Date());
        log.info("userKey:" + orderPayUserAccountDTO.getUserKey() + ", money:" + orderPayUserAccountDTO.getBrokerage());
        mallUserAccountHistory.setAppKey(null);
        return this.save(mallUserAccountHistory);
    }

    /**
     * 获取账户记录信息, 已经根据三入参，设置了唯一性索引
     *
     * @param orderKey
     * @return
     */
    @Override
    public MallUserAccountHistory getAccountOrderHistory(String orderKey) {
        return getAccountHistory(MallUserAccountHistoryConstants.SourceType.ORDER.name(), orderKey);
    }

    /**
     * 减少参数，获取用户指定的账户订单佣金收入情况
     *
     * @param sourceType
     * @param sourceKey
     * @return
     */
    public MallUserAccountHistory getAccountHistory(String sourceType, String sourceKey) {
        MallUserAccountHistory query = new MallUserAccountHistory();
        query.setSourceType(sourceType);
        query.setSourceKey(sourceKey);
        return this.getOneByEntity(query);
    }
}
